<?
require_once 'BaseAction.php';

class TaskController extends TT_Base_Action
{   
    private function time1($str)            //  tuong tu ham time()
    {
        if($str=='')
        {
            $time2=0;
        }
        else
        {
            $time1=explode("/",$str);
       
            $time2=mktime(0,0,0,$time1[1],$time1[0],$time1[2]);
        }
        return $time2;
    }
        
    private  function checkdate1($date)                       //kiem tra co phai ngay thang ko
    {
        $aDate_parts =explode("/",$date);

         if((int)$aDate_parts[0]!=0 && (int)$aDate_parts[2] !=0 && (int)$aDate_parts[2]!=0 )  
         {
            if(checkdate($aDate_parts[1],$aDate_parts[0],$aDate_parts[2]))
            {
                $a=1;
            }
            else
            {
                $a=2 ;
            }  
         }
         else
         {
            $a=0;
         }
         return $a;
    }
    
    public function indexAction()
    {
        $u = $this->getUser();
        $this->view->user=$u;  
        if ($u->uid == 0){                    //chuyen ve trang login neu chua dang nhap
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
        }     
        $params=$this->_getAllParams();
        $this->view->params=$params;                                 
    }
          
    public function newtaskAction()
    {
        $u = $this->getUser();        
        $this->view->user=$u;  
        
        if ($u->uid == 0){                    //chuyen ve trang login neu chua dang nhap
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
        }                          
        $t_model = $this->loadModel("Tasks");  
        $user_info=$t_model->checkInfo($u->uid);
        $this->view->user_info=$user_info; 
        $params=$this->_getAllParams();
        $this->view->params=$params;
    
        if ($this->getRequest()->isPost()) //neu nguoi dung submit form 
        {    
            $task_title=$params['task_title'];
            $date_start=$params['date_start'];
            $date_end=$params['date_end'];
            $status=$params['status'];
            $degree_complete=$params['degree_complete'];
            $description=$params['description'];
            $creator_id=$u->uid;
            
            $check_date_start=$this->checkdate1($date_start);              //kiem tra ngay thang
            $check_date_end=$this->checkdate1($date_end);                     //kiem tra ngay thang 
        

            if($task_title!="" && $date_end!="" && $date_start!="" && $degree_complete!="")
            {    
                if($check_date_end==1 && $check_date_start==1 && $this->time1($date_end) > $this->time1($date_start))
                {
                    if($degree_complete>=0 && $degree_complete<=100 && is_numeric($degree_complete))
                    {
                        if(($degree_complete==100 && $status=="Đã xong")||($degree_complete!=100 && $status!="Đã xong"))
                        {
                            $date_end=$this->time1($date_end);
                            $date_start=$this->time1($date_start);
                            $t_model->insert_new_task($creator_id,$task_title,$date_start,$date_end,$status,$degree_complete,$description);   
                            $this->view->thongbao5=1;      // thanh cong    
                        }
                        else
                        {
                           $this->view->thongbao4=1;          // da xong la 100
                        }
                                
                    }
                    else
                    {
                        $this->view->thongbao3=1; // khong dung %
                    }
                }
                else
                {
                    $this->view->thongbao2=1; //ko dung ding dang ngay` thang nam
                }              
            }
            else    
            {
                $this->view->thongbao1=1;      //chua dien day du thong tin
            }  
        }
    }
    
    public function edittaskAction()
    {
        $u = $this->getUser();
        $this->view->user=$u;  
        if ($u->uid == 0){                    //chuyen ve trang login neu chua dang nhap
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
        }
        $t_model = $this->loadModel("Tasks"); 
        $user_info=$t_model->checkInfo($u->uid);
        $this->view->user_info=$user_info;
        $params=$this->_getAllParams();
        $this->view->params=$params;
        
        $task_info=$t_model->task_info($params['task_id']);
        
        $task_id=$task_info->task_id;
        if($task_info->creator_id!=$u->uid)       // chuyen ve /task neu bao cao khong phai cua minh
        {
             $this->_helper->getHelper('Redirector')->gotoUrl('/task');
        }

        $this->view->task_info=$task_info;   
        
        
        if ($this->getRequest()->isPost()) //neu nguoi dung submit form 
        {    
            $task_title=$params['task_title'];
            $date_start=$params['date_start'];
            $date_end=$params['date_end'];
            $status=$params['status'];
            $degree_complete=$params['degree_complete'];
            $description=$params['description'];
            $creator_id=$u->uid;
            
            $check_date_start=$this->checkdate1($date_start);              //kiem tra ngay thang
            $check_date_end=$this->checkdate1($date_end);                     //kiem tra ngay thang 
            
            if($task_title!="" && $date_end!="" && $date_start!="" && $degree_complete!="")
            {          
                if($check_date_end==1 && $check_date_start==1 && $this->time1($date_end) > $this->time1($date_start))
                {
                    if($degree_complete>=0 && $degree_complete<=100 && is_numeric($degree_complete))
                    {
                        if(($degree_complete==100 && $status=="Đã xong")||($degree_complete!=100 && $status!="Đã xong"))
                        {
                            $date_end=$this->time1($date_end);
                            $date_start=$this->time1($date_start);
                            $t_model->edit_task($task_id,$task_title,$date_start,$date_end,$status,$degree_complete,$description); 
                            $this->view->thongbao5=1;  
                        }
                        else
                        {
                           $this->view->thongbao4=1;          // da xong la 100
                        }
                                
                    }
                    else
                    {
                        $this->view->thongbao3=1; // khong dung %
                    }
                }
                else
                {
                    $this->view->thongbao2=1; //ko dung ding dang ngay` thang nam
                }      
            }
            else    
            {
                $this->view->thongbao1=1;      //chua dien day du thong tin
            }
        }  
         $task_info=$t_model->task_info($params['task_id']);  
         $this->view->task_info=$task_info;            
    } 
    
    public function deletetaskAction()
    {
        $u = $this->getUser(); 
        $this->view->user=$u;
        $params=$this->_getAllParams();
        $this->view->params=$params;
        
        if ($u->uid == 0){                    //chuyen ve trang login neu chua dang nhap
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
        }
        $params=$this->_getAllParams(); 
        $t_model = $this->loadModel("Tasks"); 
        $user_info=$t_model->checkInfo($u->uid);
        $task_info=$t_model->task_info($params['task_id']);

        if($task_info->creator_id!=$u->uid)       // chuyen ve /task neu bao cao khong phai cua minh
        {
             $this->_helper->getHelper('Redirector')->gotoUrl('/task');
        }    
        else
        {
            $t_model->delete_task($params['task_id']);
            $this->view->thongbao1=1;
        }   
    }
    
    public function viewmytaskAction()
    {    
        $u = $this->getUser();
        $this->view->user=$u;  
        $params=$this->_getAllParams();
        $this->view->params=$params;
        if ($u->uid == 0){                    //chuyen ve trang login neu chua dang nhap
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
        }
        $t_model = $this->loadModel("Tasks");  
        
        $page_rows=20; 
        $num=$t_model->view_my_task_num($u->uid)->n;
        $this->view->num=$num;
        $num_pages=ceil($num/$page_rows);
        $this->view->num_pages=$num_pages;
    }
    
    public function mytaskAction()
    {   
        $this->_helper->layout->disableLayout();
        $u = $this->getUser();
        $this->view->user=$u;  
        if ($u->uid == 0){                    //chuyen ve trang login neu chua dang nhap
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
        }
        $t_model = $this->loadModel("Tasks");  
        $user_info=$t_model->checkInfo($u->uid);
        $this->view->user_info=$user_info; 
        $params=$this->_getAllParams();
        $this->view->params=$params;
        
        $pages=$params['page'];
        if($pages=="")
        {
            $pages=1;
        }
                $page_rows=20; 
        $page_start=($pages-1)*$page_rows;
        $this->view->page_start=$page_start;
        $page_rows=20;
        
        $mytask=$t_model->view_my_task($u->uid,$page_start,$page_rows);
        $this->view->mytask=$mytask;
        
        $num=$t_model->view_my_task_num($u->uid)->n;
        $this->view->num=$num;
        $num_pages=ceil($num/$page_rows);
        $this->view->num_pages=$num_pages;

    }
    
    public function recyclebintaskAction()
    {       
        $this->_helper->layout->disableLayout();     
        $u = $this->getUser();
        $this->view->user=$u;  
        if ($u->uid == 0){                    //chuyen ve trang login neu chua dang nhap
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
        }
        $t_model = $this->loadModel("Tasks");  
        $user_info=$t_model->checkInfo($u->uid);
        $this->view->user_info=$user_info; 
        $params=$this->_getAllParams();
        $this->view->params=$params;
        
        $pages=$params['page'];
        if($pages=="")
            {
                $pages=1;
            }
                    $page_rows=20; 
        $page_start=($pages-1)*$page_rows;
        $this->view->page_start=$page_start;
        $page_rows=20;
        
        $mytask=$t_model->view_recyclebin_task($u->uid,$page_start,$page_rows);
        $this->view->mytask=$mytask;
        
        $num=$t_model->view_recyclebin_task_num($u->uid)->n;
        $this->view->num=$num;
        $num_pages=ceil($num/$page_rows);
        $this->view->num_pages=$num_pages;
    }
    
    public function recyclebinAction()
    {
        $u = $this->getUser();
        $this->view->user=$u;  
        $params=$this->_getAllParams();
        $this->view->params=$params;
        if ($u->uid == 0){                    //chuyen ve trang login neu chua dang nhap
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
        }
        $t_model = $this->loadModel("Tasks");  
        
        $page_rows=20; 
        $num=$t_model->view_recyclebin_task_num($u->uid)->n;
        $this->view->num=$num;
        $num_pages=ceil($num/$page_rows);
        $this->view->num_pages=$num_pages;
 
    }
    
    public function recovertaskAction()
    {
        $u = $this->getUser(); 
        $this->view->user=$u;
        $params=$this->_getAllParams();
        $this->view->params=$params;
        if ($u->uid == 0){                    //chuyen ve trang login neu chua dang nhap
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
        }
        $params=$this->_getAllParams(); 
        $t_model = $this->loadModel("Tasks"); 
        $user_info=$t_model->checkInfo($u->uid);
        $task_info=$t_model->task_info($params['task_id']);


        if($task_info->creator_id!=$u->uid)       // chuyen ve /task neu bao cao khong phai cua minh
        {
             $this->_helper->getHelper('Redirector')->gotoUrl('/task');
        }    
        else
        {
            $t_model->recover_task($params['task_id']);
            $this->view->thongbao1=1;
        } 
    }
    
    public function manageAction()
    {
        $u = $this->getUser();
        $this->view->user=$u;  
        if ($u->uid == 0){                    //chuyen ve trang login neu chua dang nhap
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
        }
        
        $t_model=$this->loadModel('Tasks');
        $phong=$t_model->phong();
        $this->view->phong=$phong;
        
        $a_model=$this->loadModel('Admins');               
        $check_moderator=$a_model->checkmoderator($u->uid,5);             // kiem tra co phai mod ko 
        $this->view->check_moderator=$check_moderator;
        
        $params=$this->_getAllParams();
        $this->view->params=$params;
        
        if ($this->getRequest()->isPost()) //neu nguoi dung submit form 
        {    
            $username=$params['username'];
            $check=$t_model->checkInfo2($username);
            if($check=="")
            {
                $this->view->thongbao1=1;        // user khong ton tai

            }
            else
            {          
                $id=$params['editmanager'];
                $id_manager = $check->uid;
                $under=$params['under'];

                $t_model->editmanager($id,$id_manager);          // thay user quan ly cua phong ban
                $t_model->update_phong($id,$under);
                $this->_helper->getHelper('Redirector')->gotoUrl('/task/manage');
            }               
        }
    }           
    
    public function viewmemberAction()
    {
        $u = $this->getUser();
        $this->view->user=$u;  
        $params=$this->_getAllParams();
        $this->view->params=$params;
        if ($u->uid == 0){                    //chuyen ve trang login neu chua dang nhap
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
        }
        $t_model = $this->loadModel("Tasks");  
        
        $m=$t_model->find_manager($u->uid);
        if($m->phong_ban!="")         // kiem tra user co phai quan ly ko
        {
            $flag=1;     // la quan ly
            $this->view->flag=1;
        }
        else
        {
            $flag=0;
        }

        
        $page_rows=5; 
        $num=$t_model->listmember_num($u->uid)->n;

        $this->view->num=$num;
        $num_pages=ceil($num/$page_rows);
        $this->view->num_pages=$num_pages;   
        
        $num2=$t_model->listmember2_num($u->uid)->n;

        $this->view->num2=$num2;
        $num_pages2=ceil($num2/$page_rows);
        $this->view->num_pages2=$num_pages2;   
        
        if($flag==1)
        {
        $user_info=$t_model->find_manager($u->uid);
        $this->view->user_info=$user_info;

        $member=$t_model->listmember3($user_info->phong_ban);
        $this->view->member=$member;
        }

        
               
    }
    public function memberAction()          // hien thi danh sach nhan vien phai bao cao truc tiep' toi' toi
    {
        $this->_helper->layout->disableLayout();     
        $u = $this->getUser();
        $this->view->user=$u;  
        if ($u->uid == 0){                    //chuyen ve trang login neu chua dang nhap
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
        }
        $t_model = $this->loadModel("Tasks");  
        $user_info=$t_model->checkInfo($u->uid);
        $this->view->user_info=$user_info; 
        $params=$this->_getAllParams();
        $this->view->params=$params;
        
        $pages=$params['page'];
        if($pages=="")
            {
                $pages=1;
            }
            
                    $page_rows=5;  
        $page_start=($pages-1)*$page_rows;
        $this->view->page_start=$page_start;
        $page_rows=5;
        
   
        
        $member=$t_model->listmember($u->uid,$page_start,$page_rows);
        $this->view->member=$member;

        
        $num=$t_model->listmember_num($u->uid)->n;
        $num_pages=ceil($num/$page_rows);
        $this->view->num_pages=$num_pages;
        $this->view->num=$num;
    }
    
    public function member2Action()          // hien thi danh sach nhan vien phai bao cao dan tiep' toi' toi
    {
        $this->_helper->layout->disableLayout();     
        $u = $this->getUser();
        $this->view->user=$u;  
        if ($u->uid == 0){                    //chuyen ve trang login neu chua dang nhap
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
        }
        $t_model = $this->loadModel("Tasks");  
        $user_info=$t_model->checkInfo($u->uid);
        $this->view->user_info=$user_info; 
        $params=$this->_getAllParams();
        $this->view->params=$params;
        
        $pages=$params['page'];
        if($pages=="")
            {
                $pages=1;
            }
                    $page_rows=20; 
        $page_start=($pages-1)*$page_rows;
        $this->view->page_start=$page_start;
        $page_rows=20;
        
   
        
        $member2=$t_model->listmember2($u->uid,$page_start,$page_rows);
        $this->view->member2=$member2;

        
        $num=$t_model->listmember2_num($u->uid)->n;
        $num_pages=ceil($num/$page_rows);
        $this->view->num_pages=$num_pages;
        $this->view->num=$num;
    }
    
    public function editviewerAction()
    {
        
        $u = $this->getUser();
        $this->view->user=$u;  
        $params=$this->_getAllParams();
        $this->view->params=$params;
        
        
        if ($u->uid == 0){                    //chuyen ve trang login neu chua dang nhap
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
        }  
        $t_model=$this->loadModel('Tasks');
        
        $m=$t_model->find_manager($u->uid);
        if($m->phong_ban!="")         // kiem tra user co phai quan ly ko
        {
            $flag=1;     // la quan ly
            $this->view->flag=1;
        }
        
        $user_info=$t_model->checkInfo($params['member']);
        $this->view->user_info=$user_info;
        
        $uid=$t_model->find_uid_manager($params['member']);
        $info_manager=$t_model->checkInfo($uid->id_manager);
        $this->view->info_manager=$info_manager;
        
        $find_viewer=$t_model->find_viewer($params['member']);
        $this->view->find_viewer=$find_viewer;

        $check1=$t_model->phong2($user_info->phong_ban);

        if($check1->id_manager == $params['member'])         // neu la quan ly -> tim phong ban truc thuoc
            {
               $check2=$t_model->phong2($check1->under);
               if($check2->id_manager==$u->uid)           // neu u la quan ly phong ban truc thuoc
                {
                    $this->view->flag2=1;
                }
            }

        
        
        if($params['delete']!="")
        {
            $t_model->delete_viewer($params['member'],$params['delete']);
            $this->_helper->getHelper('Redirector')->gotoUrl('/task/editviewer?member='.$params['member']);
        }
        
        if ($this->getRequest()->isPost()) //neu nguoi dung submit form 
        {
            $name=$params['add'];
            $check=$t_model->checkInfo2($name);
            if($check=="")
            {
                $this->view->thongbao=1;
            } 
            else
            {
                $flag=0;
                for($i=0;$i<count($find_viewer);$i++)
                {
                    if($check->uid==$find_viewer[$i]->viewer_id)
                    {
                        $flag=1;
                    }
                }
                if($flag!=1)
                {
                   $t_model->add_viewer($params['member'],$check->uid);
                   $this->_helper->getHelper('Redirector')->gotoUrl('/task/editviewer?member='.$params['member']);   
                }                 
            }
        }

    }
    
    public function viewerAction()
    {
        $u = $this->getUser();
        $this->view->user=$u;  
        $params=$this->_getAllParams();
        $this->view->params=$params;
        if ($u->uid == 0){                    //chuyen ve trang login neu chua dang nhap
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
        }  
        $t_model=$this->loadModel('Tasks');

        
        $m=$t_model->find_uid_manager($u->uid);
                    
        if($m->id_manager==$u->uid)
        {
            $manager=$t_model->find_manager($u->uid);
            if($manager->under=="")
            {
                $this->view->flag1=1;     
            }
            else
            {
                $manager=$t_model->phong2($manager->under);
            }                      
        }
        else
        {
            $manager=$m;

        }
        $info_manager=$t_model->checkInfo($manager->id_manager);
        $this->view->info_manager=$info_manager;
    
        $find_viewer=$t_model->find_viewer($u->uid);    
        $this->view->find_viewer=$find_viewer; 
    }
    

    


    
 public function viewtask1Action()
    {
        $this->_helper->layout->disableLayout(); 
        $u = $this->getUser();
        $this->view->user=$u;  
        $params=$this->_getAllParams();
        $this->view->params=$params;

        if ($u->uid == 0){                    //chuyen ve trang login neu chua dang nhap
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
        }  
        $t_model=$this->loadModel('Tasks');
        
        $m=$t_model->find_manager($u->uid);
        if($m->phong_ban!="")         // kiem tra user co phai quan ly ko
        {
            $flag=1;     // la quan ly
            $this->view->flag=1;
        }

        $pages=$params['page'];
        $this->view->pages=$pages;
        if($pages=="")
            {
                $pages=1;
            }
        $page_rows=20; 
        $page_start=($pages-1)*$page_rows;
        $this->view->page_start=$page_start;

        
             
        $creator_id=$params['select1'];
        $status=$params['status'];
        $time1=$params['time1'];
        $time2=$params['time2'];
        
      
                $show1=$t_model->viewalltask4($u->uid,$creator_id,$status,$m->phong_ban,$time1,$time2,$page_start,$page_rows); 

                $this->view->show1=$show1;                     // hien thi o? phan cac bao cao truc tiep  
                $num=$t_model->count_viewalltask4($u->uid,$creator_id,$status,$m->phong_ban,$time1,$time2); 
                $this->view->num=$num->n; 
                $this->view->num_pages=ceil($num->n/$page_rows);
                

             

    }
 public function viewtaskAction()
    {
     
        $u = $this->getUser();
        $this->view->user=$u;  
        $params=$this->_getAllParams();
        $this->view->params=$params;
        
        if($params['status']=='all')
            {
                $this->view->status='all';
            }
        else
            {
                $this->view->status="'".$params['status']."'";
            }
        
        $this->select1=$params['select1'];
        
 
        if ($u->uid == 0){                    //chuyen ve trang login neu chua dang nhap
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
        }  
        $t_model=$this->loadModel('Tasks');
        $member1=$t_model->listmember_1($u->uid);
        $this->view->member1=$member1;          //danh sach nhan vien phai bao cao truc tiep
        $member2=$t_model->listmember_2($u->uid);
        $this->view->member2=$member2;     //danh sach nhanh vien duoc xem (dan tiep)
        
        $m=$t_model->find_manager($u->uid);
        if($m->phong_ban!="")         // kiem tra user co phai quan ly ko
        {
            $flag=1;     // la quan ly
            $this->view->flag=1;
        }
        if($flag==1)
        {
                    $member3=$t_model->listmember3($m->phong_ban);
                    $this->view->member3=$member3;     //danh sach nhanh vien duoc xem (dan tiep)
        }
 
        $page_rows=20;  

                
        if ($this->getRequest()->isPost()) //neu nguoi dung submit form 
        {
           $date1=trim($params['date1']);
           $date2=trim($params['date2']);
           
           
           $time1=0;
           $time2=time();

           if($date1!="")
           {
                $checkdate1=$this->checkdate1($date1);
                if($checkdate1==1)
                {
                    $time1=$this->time1($date1);
                }
                else
                {
                    $this->view->thongbao1=1;
                } 
           }
           if($date2!="")
           {
                $checkdate2=$this->checkdate1($date2);         
                 if($checkdate2==1)
                {
                    $time2=$this->time1($date2);
                }                              
                 else
                {
                    $this->view->thongbao2=1;
                }
           }
       
       $this->view->time1=$time1;
       $this->view->time2=$time2;
       
       $this->view->select1=$params['select1'];
       
       $status=$params['status'];  
       if($status!='all')
        {
            $status="'".$status."'";
        }
       $creator_id=$params['select1'];
                $num1=$t_model->count_viewalltask4($u->uid,$creator_id,$status,$m->phong_ban,$time1,$time2);
                $num_pages1=ceil($num1->n/$page_rows);
                $this->view->num_pages1=$num_pages1;

        }
        else        // neu chua an nut submit
        {
          $this->view->post=2;  
             $time=time();
             $time1=$time-2678400;     // thoi gian 1 thang trc
             $time2=$time;             // thoi gian hien tai

            $this->view->time1=$time1;
            $this->view->time2=$time2;
            $this->view->select1="all";
            $this->view->status="all";
            
            $status='all';
            $creator_id='all';
                $num1=$t_model->count_viewalltask4($u->uid,$creator_id,$status,$m->phong_ban,$time1,$time2);
                $num_pages1=ceil($num1->n/$page_rows);
                $this->view->num_pages1=$num_pages1;
                
            $this->view->thongbao3=1;
        }    
    }
}
